//
// Created by 紫霄 on 2020/4/17.
//

#ifndef LEETCODE_24_H
#define LEETCODE_24_H
#include <stack>

struct ListNode {
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
  ListNode *reverseList(ListNode *head) {
    if (!head) {
      return head;
    }
    std::stack<ListNode *> temp;
    while (head->next) {
      temp.push(head);
      head = head->next;
    }
    ListNode *newHead = head;
    while (!temp.empty()) {
      head->next = temp.top();
      head = head->next;
      temp.pop();
    }
    head->next = nullptr;
    return newHead;
  }
};
#endif // LEETCODE_24_H
